package com.example.mysimpleexample.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.example.mysimpleexample.fragment.technic.CountDownTimerFragment;
import com.example.mysimpleexample.fragment.technic.CustomSwitchFragment;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageContainerListFragment;
import com.xuexiang.xpage.base.XPageFragment;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.List;

@Page(name = "安卓使用技巧")
public class AndroidTechnicFragment extends XPageSimpleListFragment {

    @Override
    protected List<String> initSimpleData(List<String> lists) {
        lists.add("获取系统电量");
        lists.add("获取粘贴板内容");
        lists.add("获取验证码的倒计时功能");
        lists.add("自定义Switch按钮");
        return lists;
    }

    @Override
    protected void onItemClick(int position) {
        switch (position) {
            case 0:
                int percentBattery = getSystemBattery(getContext());
                ToastUtils.toast("当前电量：" + percentBattery);
                break;
            case 1:
                String content = getClipboardData();
                ToastUtils.toast(content);
                break;
            case 2:
                openPage(CountDownTimerFragment.class);
                break;
            case 3:
                openPage(CustomSwitchFragment.class);
                break;
            default:
                break;
        }
    }

    //获取手机电量
    private int getSystemBattery(Context context){
        int level = 0;
        Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        level = batteryIntent.getIntExtra("level",0);
        int batterySum = batteryIntent.getIntExtra("scale",100);
        int percentBattery = 100 * level/batterySum;

        return percentBattery;
    }

    //获取粘贴板内容
    private String getClipboardData(){
        String content = null;
        ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboardManager != null) {
            ClipData clipData = clipboardManager.getPrimaryClip();
            if (clipData != null && clipData.getItemCount() > 0) {
                ClipData.Item item = clipData.getItemAt(0);
                if (item != null) {
                    content = item.getText().toString();
                }
            }
        }
        return content;
    }
}
